package com.adinnet.admin.address.controller;


import com.adinnet.admin.address.model.Address;
import com.adinnet.admin.address.service.AddressService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 省市区 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-17
 */
@Controller
@RequestMapping("/admin/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 省下的市,或市下的区
     *
     * @return
     */
    @RequestMapping("/city/list")
    @ResponseBody
    public JsonResult addressList(String code, Integer type, HttpServletRequest request, HttpServletResponse response) {
        try {

            List<Address> addresses = addressService.addressList(code,type);
            if (CollectionUtils.isEmpty(addresses)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            return JsonResult.ok().put("data", addresses);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("省市区查询失败!");
        }
    }

}
